/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ClientGui;

import client.ForumClient;
import clientData.ForumData;
import clientData.MemberDataInt;
import clientData.PostData;
import clientData.TopicData;
import java.awt.Component;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import server.NotificationMsg;

/**
 *
 * @author iziko
 */
public class Controller {

    private  static ForumsPanel forumsPanel;
    private  static TopicsPanel topicsPanel;
    private  static PostPanel postsPanel;
    private  static ForumClient client;
    private static  LoginScrean logSc;
    private static RegistretionScr regSc;
    private static FriendScrean friends;
    private static MainScreen mainScreen;

    public Controller() {
    }

    public static void initGui(ForumClient fc) {
        client = fc;
        logSc = new LoginScrean();
        logSc.setVisible(true);
        regSc = new RegistretionScr();
        forumsPanel = new ForumsPanel();
        forumsPanel.getList().setListData(client.forums);
        forumsPanel.getList().setCellRenderer(new ForumCellRenderer());
        topicsPanel = new TopicsPanel();
        topicsPanel.getList().setListData(client.topics);
        topicsPanel.getList().setCellRenderer(new TopicCellRenderer());
        postsPanel = new PostPanel();
        postsPanel.getList().setListData(client.posts);
        postsPanel.getList().setCellRenderer(new PostCellRenderer());
        friends = new  FriendScrean();
        friends.getList().setListData(client.friends);
        friends.getList().setCellRenderer(new FriendCellRenderer());
        mainScreen = new MainScreen();
    }
    
    public static void showRegistration() {
        regSc.clearAll();
        regSc.setVisible(true);
    }

    public static void showLogin(){
        logSc.hideBadLoginMsg();
        logSc.setVisible(true);
    }

    public static void logout(){
        client.logout();
    }

    public static void register (String usr, String pass, String id, String email, String fname, String lname) {
        if (callClient(client.register(usr, pass, id, email, fname, lname),false)) {
            System.out.println("register");
            regSc.setVisible(false);
            regSc.clearAll();
            if (callClient(client.login(usr, pass),false)) {
                showMainScreen();
            }
        }
    }

    public static boolean login (String usr, String pass) {
        boolean res = client.login(usr, pass);
       if (callClient(res, false)) {
           showMainScreen();
           //mainScreen.setVisible(true);
           return true;
       }
       else {
            return false;
       }
    }

    public static void showMainScreen () {
        mainScreen.setWelcome(getUsername());
        mainScreen.setVisible(true);
        showForumList();
    }

    public static void showForumList () {
        if(callClient(client.showForums(),true)) {
            forumsPanel.getList().setListData(client.forums);
            forumsPanel.clearFields();
            mainScreen.setCurrentTitle("FORUMS");
            mainScreen.setLocationTitle("");
            mainScreen.enableForums(false);
            mainScreen.enableTopics(false);
            mainScreen.setCurrent(forumsPanel);
            forumsPanel.setVisible(true);
        }
    }

      public static void showTopicList () {
        if(callClient(client.showTopics(),true)) {
            topicsPanel.getList().setListData(client.topics);
            topicsPanel.clearFields();
            mainScreen.setCurrentTitle("TOPICS");
            String currLoc = "Forum: " + client.getCurrentForum().getName();
            mainScreen.setLocationTitle( currLoc );
            mainScreen.enableForums(true);
            mainScreen.enableTopics(false);
            mainScreen.setCurrent(topicsPanel);
            topicsPanel.setVisible(true);
          }
    }
      
    public static void showForum(ForumData forum) {
        //choose forum - set client.topics
        client.chooseForum(forum);
        showTopicList();
    }

    public static void showTopic(TopicData topic) {
        //choose topic - set client.posts
        client.chooseTopic(topic);
        showPosts();
    }

     public static void showPosts(){
         if(callClient(client.showPosts(),true)) {
             postsPanel.getList().setListData(client.posts);
             mainScreen.setCurrentTitle("POSTS");
             String currLoc = "Forum: " + client.getCurrentForum().getName() + " > Topic: " + client.getCurrentTopic().getTitle();
             mainScreen.setLocationTitle( currLoc );
             mainScreen.enableForums(true);
             mainScreen.enableTopics(true);
             mainScreen.setCurrent(postsPanel);
             postsPanel.clearFields();
             postsPanel.setVisible(true);
         }
     }

    public static void addFriend(String username){
        if (callClient(client.addFriend(username),true))
            showFriends();
    }

    public static void removeFriend(String username){
 //       System.out.print("removing friend \""+username+"\"\n");
        if(callClient(client.removeFriend(username),true))
            showFriends();
    }

     public static void showFriends() {
         if (callClient(client.showMyFriends(),true)){
             friends.getList().setListData(client.friends);
            friends.setVisible(true);
         }
    }

     public static Vector<MemberDataInt> searchFriends(String prefix){
         if (callClient(client.searchForFriend(prefix),true)){
            return client.getSearchedFriends();
         }
         return new Vector<MemberDataInt>();
     }

     public static void addForum(String name) {
         if (callClient(client.creatForum(name),true))
            showForumList();
     }

     public static void addTopic(String name, String postTitle,String postCont) {
        if(callClient(client.addTopic(name,postTitle, postCont),true))
            showTopicList();
     }

     public static void choosePost(PostData post){
         client.choosePost(post);
     }

     public static void addPost(String title, String content) {
         if(callClient(client.publishPost(title, content),true))
            postsPanel.getList().setListData(client.posts);
     }

     public static void editPost(String title, String content){
         if(callClient(client.editPost(title, content),true)){
            postsPanel.getList().setListData(client.posts);
         }
     }

     public static void removePost() {
         if(callClient(client.removePost(),true)){
            postsPanel.getList().setListData(client.posts);
         }
     }

     public static void moveToPost(NotificationMsg msg){
         if ( msg.getForum() != null) client.chooseForum(msg.getForum());
         if ( msg.getTopic() != null) client.chooseTopic(msg.getTopic());
         if ( msg.getPost() != null) {
             client.choosePost(msg.getPost());
             showPosts();
             postsPanel.setPost(msg.getPost());
         }
     }

     public static void notifyUser(String msg){
         JOptionPane.showMessageDialog(new JFrame(), msg, "Dialog",
         JOptionPane.INFORMATION_MESSAGE);
         //NotificationBox msgBox = new NotificationBox(msg);
         //msgBox.setVisible(true);
     }
     
     public static String getUsername(){
         return client.getUsername();
     }

     public static void closeAll(){
        logSc.dispose();
        regSc.dispose();
        forumsPanel.removeAll();
        topicsPanel.removeAll();
        postsPanel.removeAll();
        friends.dispose();
        mainScreen.dispose();
        System.exit(0);
     }

     private static boolean callClient(boolean res, boolean backToLogin) {
         if (res)
             return true;
         else {
                JOptionPane.showMessageDialog(new JFrame(), client.returnValue, "Dialog",
                JOptionPane.ERROR_MESSAGE);
             }
             if (!client.logged && backToLogin) {
                 showLogin();
             }
          return false;
     }

     public static void addMsg(NotificationMsg msg){
         mainScreen.addMsg(msg);
     }

     public static boolean isAdmin() {
         return client.isAdmin();
     }

     public static void refreshCurrent(){
         String current = mainScreen.getCurrent();
         if ( current.equals("FORUMS") ){
             showForumList();
             System.out.println("forums");
         }
         if ( current.equals("TOPICS")  ) {
             showTopicList();
             System.out.println("topics");
         }
         if ( current.equals("POSTS") ) {
             showPosts();
             System.out.println("posts");
         }
     }
     
}
